package cn.itcast.lucene.demo;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;

import java.io.File;

public class QueryIndex {

    public static void main(String[] args) throws Exception {

        DirectoryReader reader = DirectoryReader.open(FSDirectory.open(new File("F:\\index")));
        IndexSearcher indexSearcher = new IndexSearcher(reader);

        //Query query = new MatchAllDocsQuery();
        Query query = new TermQuery(new Term("fileName","传智播客"));
        /*Analyzer analyzer=new IKAnalyzer();
        QueryParser parser = new QueryParser("fileName",analyzer);
        Query query = parser.parse("传智播客是一家很不错的培训机构");*/
       /* Analyzer analyzer=new IKAnalyzer();
        MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[]{"fileName","fileContent"},analyzer);
        Query query = parser.parse("传智播客是一家很不错的培训机构");*/
        //NumericRangeQuery<Long> query = NumericRangeQuery.newLongRange("fileSize", 2L, 50L, true, true);
       /* BooleanQuery query = new BooleanQuery();
        TermQuery query1 = new TermQuery(new Term("fileName", "传智播客"));
        TermQuery query2 = new TermQuery(new Term("fileName", "阴阳师"));
        query.add(query1, BooleanClause.Occur.MUST);
        query.add(query2, BooleanClause.Occur.SHOULD);*/

        //WildcardQuery query = new WildcardQuery(new Term("fileName","传智播?"));

        //FuzzyQuery query = new FuzzyQuery(new Term("fileName","lucene"));

        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("总记录数："+topDocs.totalHits);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc doc : scoreDocs) {
            System.out.println("文档id："+doc.doc);
            Document document = indexSearcher.doc(doc.doc);
            //文档内容
            System.out.println("名称"+document.getField("fileName"));
            System.out.println("内容"+document.getField("fileContent"));
            System.out.println("大小"+document.getField("fileSize"));
            System.out.println("路径"+document.getField("filePath"));
        }

    }
}
